<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width,initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge,chrome=1">
        <title>leetcode 283移动零</title>
    </head>
    <body>
        <section>给定一个数组 nums，编写一个函数将所有 0 移动到数组的末尾，同时保持非零元素的相对顺序。</section>
        <h1>方法一</h1>
        <pre class="language-JavaScript">
            <code>
                var moveZeroes = function(nums) {
                    let len = nums.length;
                    for(let i = len - 1; i >= 0; i --) {
                        if(nums[i] === 0) nums.splice(i, 1);
                    }
                    for(let i = nums.length; i < len; i ++) {
                        nums.push(0);
                    }
                    return nums;
                }
            </code>
        </pre>
        <h1>方法二</h1>
        <pre class="language-JavaScript">
            <code>
                var moveZeroes = function(nums) {
                    let k =0;//nums中，[0,...,k)的元素均为非0元素
                    for(let i = 0; i < nums.length; i ++) {
                        if(nums[i]) {
                            nums[k] = nums[i];
                            k ++;
                        }
                    }
                    for(let i = k; i < nums.length; i ++) {
                        nums[i] = 0;
                    }
                    return nums;
                }
            </code>
        </pre>
        <h1>方法三</h1>
        <pre class="language-JavaScript">
            <code>
                var moveZeroes = function(nums) {
                    let k =0;//nums中，[0,...,k)的元素均为非0元素
                    for(let i = 0; i < nums.length; i ++) {
                        if(nums[i]) {
                            if(k !== i) {
                                let temp = nums[k];
                                nums[k] = nums[i];
                                nums[i] = temp;
                            }
                            k ++;
                        }
                    }
                    return nums;
                }
            </code>
        </pre>
        <script>
            var moveZeroes = function(nums) {
                let len = nums.length;
                for(let i = len - 1; i >= 0; i --) {
                    if(nums[i] === 0) nums.splice(i, 1);
                }
                for(let i = nums.length; i < len; i ++) {
                    nums.push(0);
                }
                return nums;
            }
            //O(n)时间复杂度
            //O(1)空间复杂度
            var moveZeroes = function(nums) {
                let k =0;//nums中，[0,...,k)的元素均为非0元素
                for(let i = 0; i < nums.length; i ++) {
                    if(nums[i]) {
                        nums[k] = nums[i];
                        k ++;
                    }
                }
                for(let i = k; i < nums.length; i ++) {
                    nums[i] = 0;
                }
                return nums;
            }

            var moveZeroes = function(nums) {
                let k =0;//nums中，[0,...,k)的元素均为非0元素
                for(let i = 0; i < nums.length; i ++) {
                    if(nums[i]) {
                        if(k !== i) {
                            let temp = nums[k];
                            nums[k] = nums[i];
                            nums[i] = temp;
                        }
                        k ++;
                    }
                }
                return nums;
            }
        </script>
    </body>
</html>